#include "test8.h"

/* 快速排序 */
void test8(void)
{
    int data[15] = {0};
    // int mid = 0;
    srand(time(NULL));
    for(int i = 0; i <15; i++){
        data[i] = rand()%100;
        printf("%d  ", data[i]);
    }
    printf("\n");
    // quick_sort(data, 0, 14);

    /*使用C库快排函数实现快排*/
    qsort(data, 15, sizeof(int), compare);
    for(int i = 0; i <15; i++){
        printf("%d  ", data[i]);
    }
    printf("\n");
}


int seek_mid(int *data, int low, int high)
{
    int temp = data[low];
    while( low < high ){
        while (low < high && temp <= data[high] ){
            high--;
        }
        data[low] = data[high];
        while( low < high && temp >= data[low] ){
            low++;
        }
        data[high] = data[low];
    }
    data[low] = temp;
    return low;
}

int quick_sort(int *data, int low, int high)
{
    if( low > high )
        return 0;
    
    int mid = seek_mid(data, low, high);
    quick_sort(data, low, mid-1);
    quick_sort(data, mid+1, high);

    return 0;
}

int compare(const void *p1, const void *p2)
{
    return (*(const int *)p1 - *(const int *)p2);
}